package api

import (
	"gitee.com/dreamwood/ez/server"
)

type BaseApi struct {
	RoutePrefix string
	Before      server.EzAction
	After       server.EzAction
	Operation   *server.Operation
}

func (this *BaseApi) AddRoute(url string, handler server.EzAction) *server.EzHandler {
	mainHandler := server.Do(&server.Router{Url: this.RoutePrefix + url}, handler)
	if this.Before != nil {
		mainHandler.Add(10, "before", this.Before)
	}
	if this.After != nil {
		mainHandler.Add(99, "after", this.After)
	}
	return mainHandler
}

func (this *BaseApi) SetBefore(fc server.EzAction) {
	//准备阶段，把operation 引入到Api中方便更多基础方法的整理
	this.Before = func(operation *server.Operation) {
		this.Operation = operation
		fc(operation)
	}
}

func (this *BaseApi) SetAfter(fc server.EzAction) {
	//准备阶段，把operation 引入到Api中方便更多基础方法的整理
	this.Before = func(operation *server.Operation) {
		this.Operation = operation
		fc(operation)
	}
}

func (this *BaseApi) TakeOver(operation *server.Operation) *MyOperation {
	op := new(MyOperation)
	op.Operation = operation
	return op
}
